package com.ymatou.iapi.trading.operate.testcase;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.operate.trading.api.parameter.BatchConfirmReceiveBean;
import com.ymatou.operate.trading.api.service.BatchConfirmReceiveCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.constant.ECode;

/**
 * 批量确认收货 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_BatchConfirmReceive {

    private static BatchConfirmReceiveBean batchConfirmReceiveBean;
    private static BatchConfirmReceiveCall batchConfirmReceiveCall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("批量确认收货");
    }

    @Before
    public void caseUp() {

        batchConfirmReceiveBean = new BatchConfirmReceiveBean();
        batchConfirmReceiveCall = new BatchConfirmReceiveCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(batchConfirmReceiveCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_BatchConfirmReceive_001() {
        Logger.start(true, "批量确认收货成功");
        try {
            // 参数
            Logger.comment("前置数据准备");
            ArrayList<Integer> orderIdList = new ArrayList<Integer>();
            OrderCallService orderCallService = new OrderCallService();
            // 创建订单
            for (int i = 0; i < 2; i++) {
            	PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            	Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            	int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            	int userId =placeOrderResp.getUserId();         
            	int tradingId = placeOrderResp.getTradingId();
            	// 保留两位小数
            	BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
                // 支付订单
                orderCallService.orderPayRequest(userId, totalPrice, tradingId);
                // 接单
                orderCallService.acceptOrder(orderId, sellerId);
                // 商家发货
                orderCallService.dispatched(orderId, sellerId);
                orderIdList.add(orderId);
            }
            Logger.comment("前置数据准备结束");
            batchConfirmReceiveBean.setOrderIds(orderIdList);
            batchConfirmReceiveBean.setRemark("auto batch comfirm receive");
            // 发送请求
            batchConfirmReceiveCall.setData(batchConfirmReceiveBean);
            batchConfirmReceiveCall.callService();
            Logger.verifyEquals(ECode.SUCCESS, batchConfirmReceiveCall.getCode(), "验证Code");
            DefaultWapper database = new DefaultWapper();
            // 数据库验证
            for (int i = 0; i < orderIdList.size(); i++) {
                Integer orderId = orderIdList.get(i);
                Logger.verifyEquals(4, database.getTradingStatus(orderId), "验证订单交易状态");
                Logger.verifyEquals(1, database.selectOrderTradingStatusByOrderIdAndTradingStatus(orderId, 4).size(),
                        "验证TradingStatus表中 增加一条订单操作记录");
            }
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_BatchConfirmReceive_002() {
        Logger.start(true, "订单列表中,有订单状态不能确认收货");
        try {
            // 参数
            Logger.comment("前置数据准备");
            ArrayList<Integer> orderIdList = new ArrayList<Integer>();
            OrderCallService orderCallService = new OrderCallService();
            // 创建订单
            for (int i = 0; i < 2; i++) {
            	PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            	Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            	int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            	int userId =placeOrderResp.getUserId();         
            	int tradingId = placeOrderResp.getTradingId();
            	// 保留两位小数
            	BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
                // 支付订单
                orderCallService.orderPayRequest(userId, totalPrice, tradingId);
                // 接单
                orderCallService.acceptOrder(orderId, sellerId);
                if (i == 0) {
                    // 商家发货
                    orderCallService.dispatched(orderId, sellerId);
                }
                orderIdList.add(orderId);
            }
            Logger.comment("前置数据准备结束");
            batchConfirmReceiveBean.setOrderIds(orderIdList);
            batchConfirmReceiveBean.setRemark("auto batch comfirm receive");
            // 发送请求
            batchConfirmReceiveCall.setData(batchConfirmReceiveBean);
            batchConfirmReceiveCall.callService();
            Logger.verifyEquals(ECode.SUCCESS, batchConfirmReceiveCall.getCode(), "验证Code");
            DefaultWapper database = new DefaultWapper();
            // 数据库验证
            Integer orderId = orderIdList.get(0);
            Logger.verifyEquals(4, database.getTradingStatus(orderId), "验证订单交易状态");
            Logger.verifyEquals(1, database.selectOrderTradingStatusByOrderIdAndTradingStatus(orderId, 4).size(),
                    "验证TradingStatus表中 增加一条订单操作记录");
            Integer orderId1 = orderIdList.get(1);
            Logger.verifyEquals(17, database.getTradingStatus(orderId1), "验证订单交易状态");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_BatchConfirmReceive_003() {
        Logger.start(true, "订单列表中为空");
        try {
            // 参数
            Logger.comment("前置数据准备");
            ArrayList<Integer> orderIdList = new ArrayList<Integer>();
            OrderCallService orderCallService = new OrderCallService();
            Logger.comment("前置数据准备结束");
            batchConfirmReceiveBean.setOrderIds(orderIdList);
            batchConfirmReceiveBean.setRemark("auto batch comfirm receive");
            // 发送请求
            batchConfirmReceiveCall.setData(batchConfirmReceiveBean);
            batchConfirmReceiveCall.callService();
            Logger.verifyEquals(ECode.ERROR400, batchConfirmReceiveCall.getCode(), "验证Code");
            Logger.verifyEquals("订单号不能为空", batchConfirmReceiveCall.getMsg(), "验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
}
